import { createSlice, configureStore } from '@reduxjs/toolkit'
// 创建slice
const countSlice = createSlice({
  name: 'xxx',
  initialState: {
    count: 0,
    msg: '嘿嘿嘿',
  },
  reducers: {
    add: (state: any, action: any) => {
      // reduxjs/toolkit中允许直接修改state
      state.count = state.count + action.payload
    },
    sub: (state: any, action: any) => {
      state.count = state.count - 1
    },
  },
})

// 创建store
const store = configureStore({
  reducer: {
    count: countSlice.reducer,
  },
})

// 监听redux中数据的变化
// store.subscribe(() => {
//   console.log(store.getState())
// })

// store.dispatch({ type: 'xxx/add', num: 3 })
// console.log(countSlice.actions.add(undefined))
// 注意: 如果使用slice中提供的actionCreator,传入参数的属性名固定就是payload
// store.dispatch(countSlice.actions.add(3))

export default store
